package phoenix.web.page_objects.backend;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.testng.TestException;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.web.test_objects.data_model.NotificationsAlerts;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by AlexGolovko on 07.10.2014.
 */
public class NotificationSettingsPage extends PageBaseWebExt
{

	public NotificationSettingsPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * @param p_user
	 * @param p_params
	 */
	public void setNotifications(TestUserDataModel p_user, NotificationsAlerts p_params)
	{
		try
		{
			manager.backend().searchPage().loginToBackend();
			String page = this.getUserSettings(p_user);

			Document doc = Jsoup.parse(page);
			Element loginform = (Element) doc.getElementById("edit-user_notification_subscription");
			Elements inputElements = loginform.getElementsByTag("input");
			List<String> paramList = new ArrayList<String>();

			for (Element inputElement : inputElements)
			{
				String key = inputElement.attr("name");
				String value = inputElement.attr("value");

				if (key.contains("message"))
					value = (p_params.message == true) ? "1" : "0";
				if (key.contains("wink"))
					value = (p_params.wink == true) ? "1" : "0";
				if (key.contains("browse") == true)
					value = (p_params.browse) ? "1" : "0";
				if (key.contains("new photo") == true)
					value = (p_params.newPhotoMessages) ? "1" : "0";
				if (key.contains("new member") == true)
					value = (p_params.newMemberMessages) ? "1" : "0";

				paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
			}

			String postPrams = manager.httpURLConnection().buildParams(paramList);
			manager.httpURLConnection()
					.sendPost(manager.config().BACKEND_NOTIFICATION_SETTINGS + "?userId=" + URLEncoder.encode(p_user.getId(), "UTF-8"), postPrams);

		}
		catch (Exception p_ex)
		{
			log().warn(p_ex.toString());
		}
	}

	public NotificationsAlerts getNotifications(TestUserDataModel p_user)
	{

		NotificationsAlerts results = new NotificationsAlerts();

		Document doc;
		Elements checkboxMessage;
		Elements checkboxBrowse;
		Elements checkboxWink;

		try
		{
			manager.backend().searchPage().loginToBackend();
			String html = this.getUserSettings(p_user);

			doc = Jsoup.parse(html);
			checkboxMessage = doc.getElementById("NotificationSubscriptionForm_New_message_site").getElementsByAttribute("checked");
			checkboxWink = doc.getElementById("NotificationSubscriptionForm_New_wink_site").getElementsByAttribute("checked");
			checkboxBrowse = doc.getElementById("NotificationSubscriptionForm_New_browse_site").getElementsByAttribute("checked");
		}
		catch (Exception p_ex)
		{
			throw new TestException(p_ex);
		}

		try
		{
			results.message = (checkboxMessage.size() < 1) ? false : true;
			results.wink = (checkboxWink.size() < 1) ? false : true;
			results.browse = (checkboxBrowse.size() < 1) ? false : true;
		}
		catch (Exception p_ex)
		{
			throw new TestException(p_ex);
		}
		return results;
	}

	public String getUserSettings(TestUserDataModel p_user)
	{
		try
		{
			return manager.httpURLConnection()
					.getPageContent(manager.config().BACKEND_NOTIFICATION_SETTINGS + "?userId=" + URLEncoder.encode(p_user.getId(), "UTF-8"));

		}
		catch (Exception p_ex)
		{
			throw new TestException("Error in POST REQUEST: " + p_ex);
		}
	}
}
